﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace nhom14
{
    class Program
    {	
    	static int n;
    	static sinhvien[] svien;
        static void Main(string[] args)
        {	
        	sinhvien sv = new sinhvien();
        	
        	chucnang cn = new chucnang();      	

            ConsoleKeyInfo k;
            
    	
            //--- Xay dung menu ----- 

            Console.WriteLine("Quan ly sinh vien");
            Console.WriteLine();
            Console.WriteLine("An   1:Nhap danh sach sinh vien ");
            Console.WriteLine("An   2:Tim ");
            Console.WriteLine("An   3:Luu ");
            Console.WriteLine("An   4:Hien thi thong tin");
            Console.WriteLine("An ESC:Thoat ");
           	Console.WriteLine();
            do 
            { 
                Console.WriteLine();
               	Console.Write("Nhan phim: ");
                k = Console.ReadKey();

                if (k.Key == ConsoleKey.D1)
                {
                   //Nhap so luong sinh vien
                   
                   
                   Console.WriteLine();
                   Console.Write("Nhap so luong sinh vien: ");
                   n = Convert.ToInt16(Console.ReadLine());
                   svien = new sinhvien[n];
                   
                   for (int i = 0; i < n; i++)
                   {
                   	Console.WriteLine("Nhap thong tin cho sinh vien thu " + (i + 1));
                   	svien[i] = new sinhvien();
                    svien[i].nhap();
                    
                   	
                   }
                   
                 }
                
                else if (k.Key == ConsoleKey.D2)
                {
                  //Tim
                  cn.hienthitimkiem();
                }
                else if (k.Key == ConsoleKey.D3)
                {
                  //Luu
                  cn.save();
                 /* string filePath = "D:\\k54.txt";
                          
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    for (int i = 0; i < n; i++)
                    {
                        sw.WriteLine(svien[i].get_hoten());
                        sw.WriteLine(svien[i].get_mssv());
                        sw.WriteLine(svien[i].get_namsinh());
                        sw.WriteLine(svien[i].get_dtoan());
                        sw.WriteLine(svien[i].get_dly());
                        sw.WriteLine(svien[i].get_dhoa());
                        sw.WriteLine("=================================================");
                    }
                }
            
           //Neu file khong ton tai thi tao moi
                if (!File.Exists(filePath))
                {
                }
            //Neu file da ton tai thi ghi tiep
            else
            {                
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    //w.WriteLine("Third Line");
                }
            }*/
                  
                 
                 
              
                  
                }
                 else if (k.Key == ConsoleKey.D4)
                {
                  //hienthi
                  cn.hienthithongtin();
                  
                }
                else if (k.Key == ConsoleKey.Escape)
           		{ //thoat
                Console.Clear();
                Console.WriteLine("GOODBYE!!!");
                
            	}
                else
                {	 Console.WriteLine();
                	 Console.WriteLine("Nhap khong dung ki tu, xin nhap lai");
                   
                }
            }while(k.Key != ConsoleKey.Escape);
             Console.ReadKey();
               
            }
         
        
     
    }
     public class nguoi
    {
        private string hoten;
        private string namsinh;

        public nguoi()
        {
            hoten = "";
            namsinh = "";
        }

        public nguoi(string ten, string n_sinh)
        {
            hoten = ten;
            namsinh = n_sinh;
        }

        public string get_hoten()
        {
            return hoten;
        }

        public string get_namsinh()
        {
            return namsinh;
        }

        public void set_hoten(string ht)
        {
            hoten = ht;
        }
        public void set_namsinh(string ns)
        {
            namsinh = ns;
        }       
    }

    public class sinhvien : nguoi
    {
        //du lieu
        private string MSSV;
        private int diem_toan;
        private int diem_ly;
        private int diem_hoa;

        //phuong thuc
        public sinhvien()
        {
            MSSV = "";
            diem_toan = 0;
            diem_ly = 0;
            diem_hoa = 0;
        }
        
        //ham set
        public void set_mssv(string _mssv)
        {
            MSSV = _mssv;
        }
        public void set_dtoan(int _dtoan)
        {
            diem_toan = _dtoan;
        }
        public void set_dly(int _dly)
        {
            diem_ly = _dly;
        }
        public void set_dhoa(int _dhoa)
        {
            diem_hoa = _dhoa;
        }

        //ham get
        public string get_mssv()
        {
            return MSSV;
        }
        public int get_dtoan()
        {
            return diem_toan;
        }
        public int get_dly()
        {
            return diem_ly;
        }
        public int get_dhoa()
        {
            return diem_hoa;
        }

        public void nhap()
        {
            Console.Write("Nhap ho ten: ");
            set_hoten(Console.ReadLine());
            Console.Write("Nhap nam sinh: ");
            set_namsinh(Console.ReadLine());
            Console.Write("Nhap mssv: ");
            MSSV = Console.ReadLine();
            Console.Write("Nhap diem toan: ");
            diem_toan = Convert.ToInt16(Console.ReadLine());
            Console.Write("Nhap diem ly: ");
            diem_ly = Convert.ToInt16(Console.ReadLine());
            Console.Write("Nhap diem hoa: ");
            diem_hoa = Convert.ToInt16(Console.ReadLine());
        }

        public void hienthi()
        {
            Console.WriteLine("MSSV:\t" + MSSV);
            Console.WriteLine("Ho ten:\t" + get_hoten());
            Console.WriteLine("Nam sinh:\t"+ get_namsinh());
            Console.WriteLine("Diem Toan:\t" + diem_toan);
            Console.WriteLine("Diem Ly:\t" + diem_ly);
            Console.WriteLine("Diem Hoa:\t" + diem_hoa);
           
            Console.WriteLine("_______________");
            int dtb=(diem_toan+diem_ly+diem_hoa)/3;
            Console.WriteLine("Diem trung binh: " + dtb);
            if (dtb < 5) Console.WriteLine("Xep loai hoc luc yeu");
            else if(dtb>=5 && dtb <=7) Console.WriteLine("Xep loai hoc luc trung binh");
            else if(dtb > 7 && dtb <= 8) Console.WriteLine("Xep loai hoc luc kha");
            else if (dtb > 8 && dtb <= 10) Console.WriteLine("Xep loai hoc luc gioi");
            Console.WriteLine();
        }
    }
    public class chucnang
    {
    /*_________________________________________________________________DU LIEU________________________*/
        public int soluongsv;
        public sinhvien[] sv;

    /*_________________________________________________________________CAU LENH________________________*/
        public void hienthiMENU()
        {
            Console.WriteLine("-- --- =====|   =======================================");
            Console.WriteLine("-- --- > M =|   An phim 1: Nhap");
            Console.WriteLine("-- --- M E =|   An phim 2: Tim kiem");
            Console.WriteLine("-- --- E N =|   An phim 3: Save");
            Console.WriteLine("-- --- N U =|   An phim 4: Hien thi");
            Console.WriteLine("-- --- U < =|   An phim ESC: Thoat");
            Console.WriteLine("-- --- =====|   =======================================");
            Console.Write("Chon: ");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            if (info.Key == ConsoleKey.D1)
            {
                //HAM NHAP    
                //Nhap so luong sinh vien    
                Console.Clear();       
                Console.Write("Nhap so luong sinh vien: ");
                soluongsv = Convert.ToInt32(Console.ReadLine());
                sv = new sinhvien[soluongsv];
                //Nhap tung thanh vien
                for (int i = 0; i < soluongsv; i++)
                {
                    Console.WriteLine("====================================================");
                    Console.WriteLine("Nhap thong tin cho sinh vien thu " + (i + 1));
                    sv[i] = new sinhvien();
                    sv[i].nhap();
                    
                }
                //Thong bao nhap xong
                Console.Beep(250, 100);
                Console.WriteLine("\nFINISHED!!!");
                delay();
                Console.Clear();
                hienthiMENU();
            }
            else if (info.Key == ConsoleKey.D2)
            {
                //ham tim kiem
                Console.Clear();
                hienthitimkiem();
            }
            else if (info.Key == ConsoleKey.D3)
            {
                //ham save                
                save();
            }
            else if (info.Key == ConsoleKey.D4)
            {
                //ham hien thi thong tin
                hienthithongtin();
            }
            //ESC
            else if (info.Key == ConsoleKey.Escape)
            {
                tambiet();
                //thoat
            }
            //An phim khac
            else
            {
                nhapsai();
            }
        }
      
        //HIEN THI TIM KIEM
        public void hienthitimkiem()
        {
            Console.Clear();
            Console.WriteLine(" *      TIM KIEM      *\n");
            Console.WriteLine(" Nhan 1: Theo MSSV");
            Console.WriteLine(" Nhan 2: Theo Ten");
            Console.WriteLine(" Nhan 3: Diem trung binh");
            Console.WriteLine(" Nhan 4: Theo xep loai");
            Console.WriteLine(" Nhan 5: Quay ve trang chu");
            Console.WriteLine(" Nhan ESC de thoat");
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.Key == ConsoleKey.D1)
            {
                //tim kiem theo mssv
                Console.Write("\n\nNhap MSSV de tim kiem: ");
                string temp = Console.ReadLine();
                for (int i = 0; i < soluongsv; i++)
                {
                    if (sv[i].get_mssv() == temp)
                    {
                        sv[i].hienthi();
                    }
                }
                delay();
                hienthitimkiem();
            }
            else if (info.Key == ConsoleKey.D2)
            {
                //tim kiem theo ten
                Console.Write("\n\nNhap Ten de tim kiem: ");
                string temp = Console.ReadLine();
                Console.WriteLine();
                for (int i = 0; i < soluongsv; i++)
                {
                    if (sv[i].get_hoten() == temp)
                    {
                        sv[i].hienthi();
                    }
                }
                delay();
                hienthitimkiem();
            }
            else if (info.Key == ConsoleKey.D3)
            {
                //tim kiem sv theo diem trung binh
                Console.Write("\n\nNhap Dien trung binh de tim kiem: ");
                double temp = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine();
                for (int i = 0; i < soluongsv; i++)
                {
                    if ((sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa())/3 == temp)
                    {
                        sv[i].hienthi();
                    }
                }
                delay();
                hienthitimkiem();
            }
            else if (info.Key == ConsoleKey.D4)
            {
                //tim kiem sinh vien theo xep loai
                timkiemtheoxeploai();
                delay();
            }
            else if (info.Key == ConsoleKey.D5)
            {
                //back
                Console.Clear();
                hienthiMENU();
            }
            else if (info.Key == ConsoleKey.Escape)
            {
                //thoat
                tambiet();
            }
            else
            {
                //bao nhap sai
                nhapsai();
            }
        }

        //SAVE
        public void save()
        {
            string filePath = "D:\\k54.txt";
                          
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    for (int i = 0; i < soluongsv; i++)
                    {
                        sw.WriteLine(sv[i].get_hoten());
                        sw.WriteLine(sv[i].get_mssv());
                        sw.WriteLine(sv[i].get_namsinh());
                        sw.WriteLine(sv[i].get_dtoan());
                        sw.WriteLine(sv[i].get_dly());
                        sw.WriteLine(sv[i].get_dhoa());
                        sw.WriteLine("=================================================");
                    }
                }
            
           //Neu file khong ton tai thi tao moi
                if (!File.Exists(filePath))
                {
                }
            /*//Neu file da ton tai thi ghi tiep
            else
            {                
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    //w.WriteLine("Third Line");
                }
            }*/
        }

        //HIEN THI THONG TIN
        public void hienthithongtin()
        {
            Console.Clear();
            Console.WriteLine("Chon kieu hien thi thong tin:\n");
            Console.WriteLine("Nhan 1: Theo diem trung binh tang dan");
            Console.WriteLine("Nhan 2: SV co diem ca 3 mon >= 5");
            Console.WriteLine("Nhan 3: Quay lai trang truoc");
            Console.WriteLine("Nhan ESC de Thoat");
            Console.WriteLine("__________________________________________________\n");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            if (info.Key == ConsoleKey.D1)
            {
                //Ham hien thi theo diem trung binh tang dan
                hienthi_sv_theo_dtb_tangdan();
                delay();
                hienthithongtin();
            }
            else if (info.Key == ConsoleKey.D2)
            {
                //Ham hien thi sinh vien co diem ca 3 mon >= 5
                hienthi_sv_co_diem_3_mon_lonhon_hoac_bang5();
                delay();
                hienthithongtin();
            }
            else if (info.Key == ConsoleKey.D3)
            {
                Console.Clear();
                hienthiMENU();
            }
            else if (info.Key == ConsoleKey.Escape)
            {
                tambiet();
                //thoat
            }
            else
            {
                nhapsai();
            }
        }

        //Ham hien thi theo diem trung binh tang dan
        public void hienthi_sv_theo_dtb_tangdan()
        {
            int i, j;
            sinhvien[] tempsv = new sinhvien[soluongsv];
            sinhvien temp = new sinhvien();
            tempsv = sv;
            for (i = 0; i < (soluongsv - 1); i++)
            {
                double dtb_i = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                for (j = i; j < soluongsv; j++)
                {
                    double dtb_j = (sv[j].get_dtoan() + sv[j].get_dly() + sv[j].get_dhoa()) / 3;
                    if (dtb_i > dtb_j)
                    {
                        temp = tempsv[j];
                        tempsv[i] = tempsv[j];
                        tempsv[j] = temp;
                    }
                }
            }
            Console.WriteLine("DANH SACH SINH VIEN:");
            for (i = 0; i < soluongsv; i++)
            {
                tempsv[i].hienthi();
            }
            delay();
            hienthithongtin();
        }

        //Ham hien thi sinh vien co diem ca 3 mon >=5
        public void hienthi_sv_co_diem_3_mon_lonhon_hoac_bang5()
        {
            for (int i = 0; i < soluongsv; i++)
            {
                if ((sv[i].get_dly() >= 5) && (sv[i].get_dtoan() >= 5) && (sv[i].get_dhoa() >= 5))
                {
                    sv[i].hienthi();
                    Console.WriteLine();
                }
            }
        }

        //Tim kiem sinh vien theo xep loai        
        public void timkiemtheoxeploai()
        {
            Console.Clear();
            Console.WriteLine("Nhan 1: Yeu");
            Console.WriteLine("Nhan 2: Trung binh");
            Console.WriteLine("Nhan 3: Kha");
            Console.WriteLine("Nhan 4: Gioi");
            Console.WriteLine("Nhan 5: De quay lai trang truoc");
            Console.WriteLine("Nhan ESC de thoat");
            Console.WriteLine("__________________________________________________\n");
            ConsoleKeyInfo info = Console.ReadKey();
            Console.WriteLine();
            //Tim kiem sinh vien co hoc luc yeu
            if (info.Key == ConsoleKey.D1)
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa())/3;
                    if (diemtrungbinh < 5)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            //Tim kiem sinh vien co hoc luc trung binh
            else if (info.Key == ConsoleKey.D2)
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh >= 5 && diemtrungbinh <= 7)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            //Tim kiem sinh vien co hoc luc kha
            else if (info.Key == ConsoleKey.D3)
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh > 7 && diemtrungbinh <= 8)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            //Tim kiem sinh vien co hoc luc gioi
            else if (info.Key == ConsoleKey.D4)
            {
                for (int i = 0; i < soluongsv; i++)
                {
                    double diemtrungbinh = (sv[i].get_dtoan() + sv[i].get_dly() + sv[i].get_dhoa()) / 3;
                    if (diemtrungbinh >=8 && diemtrungbinh <= 10)
                    {
                        sv[i].hienthi();
                        Console.WriteLine();
                    }
                }
            }
            else if (info.Key == ConsoleKey.D5)
            {
                //back
                hienthitimkiem();
            }
            else if (info.Key == ConsoleKey.Escape)
            {
                tambiet();
            }
            else
            {
                nhapsai();
                timkiemtheoxeploai();
            }
            //Sau khi tim kiem xong
            Console.WriteLine("_____________________________________________________________________");
            delay();            
            timkiemtheoxeploai();                            
        }
           
        //Delay
        public void delay()
        {
            Console.WriteLine("\nAN PHIM BAT KI DE TIEP TUC!");
            Console.ReadKey();       
        }
        
        //HAM CHAO TAM BIET
        public void tambiet()
        {
            Console.Clear();
            Console.WriteLine("GROUP 7 SAY \"GOODBYE\" TO YOU! SEE YOU AGAIN!");
            for (soluongsv = 1; soluongsv < 30; soluongsv++)
            {
                Console.Beep(soluongsv * 200, 100);
            }
        }

        //nhap bao nhap sai
        public void nhapsai()
        {
            Console.Clear();
            Console.Beep();
            Console.WriteLine("Not avaiable! Try again, plz\n");
            hienthiMENU();
        }

        //chuc nang chua hoan thien
        public void chuaxong()
        {
            Console.Clear();
            Console.WriteLine("\nChuc nang dang duoc hoan thien");
            Console.Beep(37, 3000);
            Console.Clear();
            hienthiMENU();
        }

        public bool file { get; set; }
    }
    
       
    
    
    
  
}



          